package search.linearsearch;

import search.generator.ArrayGenerator;

/**
 * @author XY
 * @version 1.0
 * @date 2021/6/20 14:26
 * @Description 线性查找
 */
public class LinearSearch {

    private LinearSearch() {
    }

    public static void main(String[] args) {


        int n = 1000000;
        Integer[] data = ArrayGenerator.generateOrderedArray(n);
        long start = System.nanoTime();
        //测试算法性能
        for(int i=0; i<100; i++){
            int res = LinearSearch.search(data, n);
        }
        long end = System.nanoTime();
        System.out.println((end-start)/1000000000.0+"s");
//        System.out.println(res);
//        int res1 = LinearSearch.search(data, 666);
//        System.out.println(res1);

//        Student[] students= {new Student("xy"),new Student("cmy"),new Student("cc")};
//        Student xy=new Student("cmy");
//        Integer search = LinearSearch.search(students, xy);
//        System.out.println(search);

    }

    //泛型只能接受类对象，不能接受基本数据类型
    public static <E> Integer search(E[] data, E target) {
        for (int i = 0; i < data.length; i++) {
            //对象不能用==号进行比较，要使用equals，可以重写equals方法控制判断的依据是什么
            if (data[i].equals(target)) {
                return i;
            }
        }
        return -1;
    }
}
